package org.alex.samples.web;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpSession;

//import org.alex.samples.model.ExamUserRole;
import org.alex.samples.model.ExamUserRoleVO;
import org.alex.samples.model.ExamUserVO;
import org.alex.samples.service.IUserJdbcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class LoginController {

	@Autowired
	private IUserJdbcService examUserService;

	// 退出登录页面
	@RequestMapping(value = "/logout")
	public String logout(HttpSession session) {
		session.removeAttribute("examUser");
		return "redirect:/login";
	}
	
	
	// 进入登录页面
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String toLogin() {
		return "login/login";
	}
	
	// 进行登录
		// value object
		@RequestMapping(value = "/login", method = RequestMethod.POST)
		public String doLogin(ExamUserVO vo, HttpSession session) {
			if (vo != null) {
				ExamUserVO resultVO = examUserService.getUserJdbcById1(vo.getUserId());
				//ExamUserVO resultVO = examUserService.getUserJdbcById1(vo.getUserName());
				//获取中间表
				ExamUserRoleVO examUserRole=examUserService.getExamUserRole(resultVO.getUserId());
				if(examUserRole != null){
					resultVO.setRoleId(examUserRole.getRoleId());
				}else{
					return null;
				}
							
				if (resultVO != null) {
					if (vo.getPassword() != null) {
						//String encodePassword = MD5Utils.GetMD5Code(vo.getPassword());
						String encodePassword = vo.getPassword();
						// 密码正确的情况下
						Date currentTime = null;
						Calendar currentTimecalendar = Calendar.getInstance(); // 创建日历对象
						currentTimecalendar.setTime(new Date());
						currentTimecalendar.add(Calendar.MINUTE, -5);
						currentTime = currentTimecalendar.getTime();
						System.out.println(currentTime + "---------------------" + new Date());
						
						if (resultVO.getLastLockTime().before(currentTime)) {						
							// 不在锁定时间范围内
							System.out.println("不在锁定时间范围内");
							if (encodePassword.equals(resultVO.getPassword())) {
								// 登录成功时请0
								resultVO.setLoginFailCount(0);
								examUserService.updateUserLoginFailCount(resultVO);
								session.setAttribute("examUser", resultVO);								
								return "decorators/decorators";
							} else {
								resultVO.setLoginFailCount(resultVO.getLoginFailCount() + 1);
								if (resultVO.getLoginFailCount() == 3) {
									resultVO.setLoginFailCount(0);
									resultVO.setLastLockTime(new Timestamp(System.currentTimeMillis()));
									examUserService.updateUserLastLockTime(resultVO);
								}
								examUserService.updateUserLoginFailCount(resultVO);								
							}
						} 
						else {
							// 锁定时间范围内
							System.out.println("目前账号被锁定，无法登录，请稍等5分钟。");
						}
					}
				}
			}
			
				return "login/login";
		}
		
	
}
	
	
	
	
	

